<template>
	<view class="container">
		<view class="main-wrap" v-if="list.length > 0">
			<view class="item column" v-for="(item, index) in list" :key="index" @click="chooseAddress(item)">
				<view class="row">
					<text class="name">{{ item.pickupName }}</text>
					<text class="mobile">{{ item.pickupMobile }}</text>
				</view>
				<text class="content">{{ item.province }} {{ item.city}}{{ item.district }} {{ item.addressDet}}</text>
				<view class="bot row b-t" @click.stop.prevent="stopPrevent">
					<view class="default-address">
						<text class="iconfont iconxuanzhong" :class="{active:item.isDefault === 1}"
							@click.stop.prevent="setDefaultAddress(item)"></text>
						<text>默认地址</text>
					</view>
					<view class="btn center" @tap="showDelModal(item)">
						<text class="iconfont iconshanchu"></text>
						<text>删除</text>
					</view>
					<view class="btn center" @tap="editAddress('update',item)">
						<text class="iconfont iconbianji1"></text>
						<text>编辑</text>
					</view>
				</view>
			</view>
			<view class="bot-btn-wrap fix-bot bg-none">
				<mix-button text="新增收货地址" icon="icon-jia2" :iconSize="36" :isConfirm="false"
					@onConfirm="editAddress('add')"></mix-button>
			</view>
		</view>
		<tui-modal :show="modalShow" @click="handleClick" @cancel="hide" title="提示" :content="modalContent"></tui-modal>
		<loading v-if="isLoading"></loading>
		<mix-empty v-else-if="!isLoading && list.length === 0" :type="'address'" :backgroundColor="'#fff'"></mix-empty>
		<tui-modal></tui-modal>
	</view>
</template>

<script>
	import mixButton from '@/components/mix-button/mix-button.vue'
	import mixEmpty from '@/components/mix-empty/mix-empty.vue'
	// import mixin from "@/utils/mixin.js"

	export default {
		// mixins: [mixin],
		components: {
			mixEmpty,
			mixButton
		},
		data() {
			return {
				isLoading: false,
				list: [],
				modalContent: "确认要删除地址吗？",
				modalShow: false,
				curIndex: 0,
				delItem: null,
				submitOrderFlag: '',
			}
		},
		onLoad(options) {
			// #ifdef H5
			this.$wechat.noShare();
			// #endif
			// this.list=JSON.parse(options.addressData);
			this.getAddress()
			if (options.submitOrderFlag == 1) {
				this.submitOrderFlag = options.submitOrderFlag
			}
		},
		methods: {
			getAddress() { //获取用户信息
				this.isLoading = true;
				let params={
					type:2,
				}
				this.$http.customersIncipient(params).then((res) => {
					this.isLoading = false;
					if (res.status === 0) {
						if (res.data) {
							this.list = res.data.customersAddressData;
						}
					}
				})
			},
			showDelModal(item) { //确认删除弹框
				this.modalShow = true;
				this.delItem = item;
			},
			handleClick(e) {
				let index = e.index;
				if (index === 1) {
					let id = this.delItem.customerAddressId;
					this.$http.deleteAddress(id).then((res) => {
						if (res.status === 0) {
							this.hide();
							this.getAddress();
							setTimeout(function() {
								uni.showToast({
									title: "删除成功!",
									icon: "none",
									duration: 500
								});
							}, 200)
						}
					})
				}
				this.hide();
			},
			hide() {
				this.modalShow = false;
			},
			editAddress(type, item) {
				if (type === 'update') {
					this.navTo(`/packageA/pages/editAddress/editAddress?id=${item.customerAddressId}`);
				} else {
					this.navTo('/packageA/pages/editAddress/editAddress');
				}

			},

			setDefaultAddress(item) { //设置默认
				if (item.isDefault === 1) {
					uni.showToast({
						title: "已经是默认地址了!",
						icon: "none",
						duration: 500
					});
					return;
				}
				item.isDefault = 1;
				let params = item;
				params.ifEdit = "default";
				this.$http.updateAddress(params).then((res) => {
					if (res.status === 0) {

						this.getAddress();
						setTimeout(function() {
							uni.showToast({
								title: "默认地址设置成功!",
								icon: "none",
								duration: 500
							});
						}, 200)
					}
				})

			},
			//选择地址
			chooseAddress(item) {
				// if(!this.isChoose){
				// 	return;
				// }
				// this.$util.prePage().setAddress(item);
				// uni.navigateBack();
				if (this.submitOrderFlag == 1) {
					// this.$util.prePage().getAddress();
					uni.setStorageSync('addressId', item.customerAddressId)
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
					// uni.redirectTo({
					// 	url:'../submitOrder/submitOrder'
					// })
				}
			},
			stopPrevent() {},
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style scoped lang="scss">
	.container {
		padding-top: 20rpx;
		padding-bottom: 130rpx;
	}

	.main-wrap {
		// opacity: 0;
		transition: opacity .2s;

		&.show {
			opacity: 1;
		}
	}

	.item {
		padding: 30rpx 28rpx 0;
		margin: 0 20rpx 20rpx;
		background-color: #fff;
		border-radius: 12rpx;
		box-shadow: 0rpx 0rpx 20rpx rgba(0, 0, 0, .03);

		.name {
			margin-right: 28rpx;
			font-size: 32rpx;
			color: #333;
		}

		.mobile {
			margin-right: 32rpx;
			font-size: 28rpx;
			color: #999;
		}

		.tag {
			padding: 0 8rpx;
			font-size: 20rpx;
			color: #fff;
			line-height: 28rpx;
			// background-color: $base-color;
			border-radius: 2rpx;
		}

		.content {
			width: 560rpx;
			min-height: 70rpx;
			margin: 28rpx 0 20rpx;
			font-size: 28rpx;
			color: #999;
			line-height: 1.44;
		}

		.bot {
			justify-content: flex-end;
			height: 79rpx;
			position: relative;

			&:before {
				left: -26rpx;
				right: -26rpx;
				border-color: #f0f0f0;
			}
		}

		.default-address {
			position: absolute;
			left: 0rpx;
			font-size: 26rpx;

			.iconfont {
				padding: 14rpx 0 14rpx 14rpx;
				color: #ccc;

				&.active {
					color: $themeColor1;
				}
			}
		}

		.btn {
			margin-left: 15rpx;
			font-size: 26rpx;
			color: #444;

			.iconfont {
				margin-right: 8rpx;
				font-size: 34rpx;
			}
		}
	}
</style>
